<template>
  <div class="app-container">
    <!-- 搜索栏开始 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      style="margin-top: -15px"
    >
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          style="width: 260px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 260px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 搜索栏结束 -->

    <!-- 按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 10px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="openSelectUser"
          >添加用户
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          >批量取消授权
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Close" @click="handleClose"
          >关闭
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>
    <!-- 按钮结束 -->

    <!-- 表格数据开始 -->
    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="用户编号"
        prop="userId"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="用户名称"
        prop="username"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns[1].visible"
      />
      <el-table-column
        label="用户昵称"
        prop="nickName"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="邮箱"
        prop="email"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="手机号码"
        prop="phoneNumber"
        align="center"
        :show-overflow-tooltip="true"
        v-if="columns[4].visible"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        v-if="columns[5].visible"
      >
        <template #default="scope">
          <el-tag :type="scope.row.status === 0 ? 'info' : ''">
            {{ getRoleStatus(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        v-if="columns[6].visible"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        v-if="columns[7].visible"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="CircleClose"
            @click="cancelAuthUser(scope.row)"
            >取消授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格数据结束 -->

    <!-- 分页开始 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 分页结束 -->
    <select-user
      ref="selectRef"
      :roleId="queryParams.roleId"
      @ok="handleQuery"
    />
  </div>
</template>

<script setup name="AuthUser">
import { getCurrentInstance, reactive, ref } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
import router from "@/router";
import { ElMessageBox } from "element-plus";
import * as roleApi from "@/api/system/roleApi";
import SelectUser from "./SelectUser";

const store = useStore();
const route = useRoute();
const { proxy } = getCurrentInstance();

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);

const roleStatusList = ref([
  { key: 0, value: 0, label: "停用" },
  { key: 1, value: 1, label: "正常" },
]);
const columns = ref([
  { key: 0, label: "用户编号", visible: true },
  { key: 1, label: "用户名称", visible: true },
  { key: 2, label: "用户昵称", visible: true },
  { key: 3, label: "邮箱", visible: true },
  { key: 4, label: "手机", visible: true },
  { key: 5, label: "状态", visible: true },
  { key: 6, label: "创建时间", visible: true },
  { key: 7, label: "更新时间", visible: true },
]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  username: undefined,
  phoneNumber: undefined,
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  roleApi
    .getRoleAssignedUserList(queryParams)
    .then((res) => {
      console.log(res.data);
      userList.value = res.data.rows;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 根据角色状态值返回对应角色状态 */
function getRoleStatus(status) {
  let statusName = "";
  roleStatusList.value.some((item) => {
    statusName = item.label;
    return status === item.value;
  });
  return statusName;
}

/** 返回按钮  */
function handleClose() {
  const obj = { path: "/system/role" };
  store.dispatch("tagsView/delView", router.currentRoute.value);
  if (obj) {
    router.push(obj);
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.userId);
  multiple.value = !selection.length;
}

/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs.selectRef.show();
}

/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  ElMessageBox.confirm(
    `确认要取消该用户"${row.username}"角色吗？`,
    "系统提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => roleApi.deleteRoleOfSomeUser({ userIds: row.userId }))
    .then(() => {
      getList();
    })
    .catch(() => {});
}

/** 批量取消授权按钮操作 */
function cancelAuthUserAll() {
  const { roleId } = queryParams;
  const uIds = userIds.value.join(",");
  ElMessageBox.confirm("是否取消选中用户授权数据项?", "系统提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => roleApi.deleteRoleOfSomeUser({ roleId, userIds: uIds }))
    .then(() => {
      getList();
    })
    .catch(() => {});
}

getList();
</script>
